package com.zyao89.view.zweb.javascript;


import com.alibaba.fastjson.JSONObject;
import com.zyao89.view.zweb.constants.InternalConstantName;
import com.zyao89.view.zweb.constants.InternalFunctionName;
import com.zyao89.view.zweb.inter.IZWebHandler;
import com.zyao89.view.zweb.inter.IZWebMethodInterface;
import com.zyao89.view.zweb.utils.JsUtils;

/**
 * 扩展暴露接口
 *
 * @author Zyao89
 * 2017/11/6.
 */
public class ZJavaScriptEx extends ZJavaScript implements IZMethodCallback {
    public ZJavaScriptEx(IZWebHandler web) {
        super(web);
    }

    @Override
    public void saveData(String frameworkID, String oJson) {
        JSONObject jsonObject = JsUtils.json2Obj(oJson);
        final String sequence = jsonObject.getString(InternalConstantName.SEQUENCE);
        final JSONObject oData = jsonObject.getJSONObject(InternalConstantName.DATA);
        final String key = oData.getString(InternalConstantName.PARAM_KEY);
        final String value = oData.getString(InternalConstantName.PARAM_VALUE);

        final IZWebMethodInterface.IZDatabaseController zController = new ZDatabaseController(InternalFunctionName.DATABASE_CALLBACK, sequence);
        getZWebMethodInterface().saveData(getZWebHandler(), key, value, zController);
    }

    @Override
    public void loadData(String frameworkID, String oJson) {
        JSONObject jsonObject = JsUtils.json2Obj(oJson);
        final String sequence = jsonObject.getString(InternalConstantName.SEQUENCE);
        final String key = jsonObject.getString(InternalConstantName.DATA);

        final IZWebMethodInterface.IZDatabaseController zController = new ZDatabaseController(InternalFunctionName.DATABASE_CALLBACK, sequence);
        getZWebMethodInterface().loadData(getZWebHandler(), key, zController);
    }

    @Override
    public void showLoading(String frameworkID) {
        getZWebMethodInterface().showLoading(getZWebHandler());
    }

    @Override
    public void hideLoading(String frameworkID) {
        getZWebMethodInterface().hideLoading(getZWebHandler());
    }

    @Override
    public void tip(String frameworkID, String msg) {
        getZWebMethodInterface().tip(getZWebHandler(), msg);
    }

    class ZDatabaseController extends ZRequireController implements IZWebMethodInterface.IZDatabaseController {
        ZDatabaseController(String function, String sequence) {
            super(function, sequence);
        }
    }
}
